package exec;

import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author fromenje & fraissea
 */
public class Comm extends Thread {

    Socket socket = null;
    Message message = null;
    DataInputStream dataInput = null;
    DataOutputStream dataOutput = null;
    FileInputStream fileInput = null;
    FileOutputStream fileOutput = null;
    Object whoCalls = null;
    File fileIn;
    File fileOut;

    public DataInputStream getDataInput() {
        return dataInput;
    }

    public void setDataInput(DataInputStream dataInput) {
        this.dataInput = dataInput;
    }

    public DataOutputStream getDataOutput() {
        return dataOutput;
    }

    public void setDataOutput(DataOutputStream dataOutput) {
        this.dataOutput = dataOutput;
    }

    public FileInputStream getFileInput() {
        return fileInput;
    }

    public void setFileInput(FileInputStream fileInput) {
        this.fileInput = fileInput;
    }

    public FileOutputStream getFileOutput() {
        return fileOutput;
    }

    public void setFileOutput(FileOutputStream fileOutput) {
        this.fileOutput = fileOutput;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public void send() {
        message.send(getDataOutput());
    }

    public void send(Message ms) {
        ms.send(getDataOutput());
    }

    @Override
    public void run() {
        while ( !(socket.isClosed()))  {
            message.read(dataInput);
            if (whoCalls instanceof Server) {
                try {
                    ((Server) whoCalls).parseMessage(this);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Comm.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else if (whoCalls instanceof Client) {
                try {
                ((Client) whoCalls).parseMessage(this);
                } catch (Exception ex) {
                    Logger.getLogger(Comm.class.getName() + "erreur client");
                }
            }
        }
    }

    public Comm(Socket socket, Object whoCalls) {
        try {
            this.socket = socket;
            this.whoCalls = whoCalls;
            dataInput = new DataInputStream(socket.getInputStream());
            dataOutput = new DataOutputStream(socket.getOutputStream());
            message = new Message();
        } catch (IOException ex) {
            Logger.getLogger(Comm.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

        synchronized void close() {
        try {
            socket.close();
            dataInput.close();
            dataInput = null;
            dataOutput.close();
            dataOutput = null;
            
            //socket = null;

        } catch (Throwable ex) {
            Logger.getLogger(Comm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
